<?php
require_once("inc.member.php");

$this_title="$vars[member_title] &raquo; ".__("Stockist Group Sales");
$content_title = $page_title=__("Stockist Group Sales");

//#####LIST STOCKIST SALES#####
$count_sql="select count(*) from $db->users where stockist_uid=".$uid;
$record_sql="select a.*,sum(b.bv) as ttl_bv, sum(b.sales_bv) as ttl_sales_bv from $db->users a left join $db->add_stockist_sales b on a.id=b.uid and a.stockist_uid=b.stockist_uid and a.stockist_assign_date=b.stockist_assign_date where a.stockist_uid=".$uid." group by a.id";

$cur_page=$_GET["nav"];
$total_record=@mysql_result(mysql_query($count_sql), 0);
$total_page=ceil($total_record/$item_per_page);
$page=($cur_page? ($cur_page>$total_page? $total_page : ($cur_page<1? 1 : $cur_page)) : 1);
$limit_start=($page-1)*$item_per_page;
$nav_link=format_page($page, $total_page, 20);
$this_page_total=@mysql_num_rows($r=mysql_query($record_sql));
$k=0;
$ttl_sales=0;
$ttl_incentive=0;
for($i=0;$i<$this_page_total;$i++){
	$run_no = (($page - 1) * $item_per_page) + $i + 1;
	$stockist=mysql_fetch_assoc($r);
	$sale_row.="
	<tr class='row$k'>
		<td>$run_no</td>
		<td>".$stockist["stockist_code"]."</td>
		<td align='right'>".number_format($stockist["ttl_bv"]+$stockist["ttl_sales_bv"],2)."</td>
		<td align='right'>".number_format(($stockist["ttl_bv"]+$stockist["ttl_sales_bv"])*0.05,2)."</td>
	</tr>";
	$k=1-$k;
	$ttl_sales+=$stockist["ttl_bv"]+$stockist["ttl_sales_bv"];
	$ttl_incentive+=($stockist["ttl_bv"]+$stockist["ttl_sales_bv"])*0.05;
}


//listing
$sales_list=($errmsg || $msg? $errmsg.$msg : "").
"<p class='center'></p>
$search_ui<br />\n
$listing_text $nav_link
<table class='pbl_table'>
	<tr class='pbl_header'>		
		<td>".__("No")."</td>
		<td>".__("Stockist Code")."</td>
		<td align='right'>".__("Group Sales BV")."</td>
		<td align='right'>".__("Incentive")."</td>
	</tr>
	$sale_row
	<tr class='pbl_header'>		
		<td align='right' colspan=2>".__("Total")."</td>
		<td align='right'>".number_format($ttl_sales,2)."</td>
		<td align='right'>".number_format($ttl_incentive,2)."</td>
	</tr>
</table>
</form>";
//#####END MEMBER STOCKIST LIST#####

$content="<h2>$page_title</h2>".$sales_list;

print format_member_page($content, $this_title, $content_title, $css);

?>